package com.web.dao;

import com.github.pagehelper.Page;
import com.web.controller.web.SysController;
import com.web.dto.MerchantUser;
import com.web.entity.User;
import com.web.entity.UserExample;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * @author FDjavaone
 */
public interface UserMapper {
    @SelectProvider(type=UserSqlProvider.class, method="countByExample")
    int countByExample(UserExample example);

    @DeleteProvider(type=UserSqlProvider.class, method="deleteByExample")
    int deleteByExample(UserExample example);

    @Delete({
        "delete from t_user",
        " where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into t_user (username, password, ",
        " age, gender, phone, ",
        " email, QQ, description, ",
        " status, account_non_Locked, ",
        " credentials_non_expired, account_non_expired, ",
        " enabled, create_time, ",
        " modify_time, role_id, ",
        " lastPasswordResetDate)",
        " values (#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, ",
        " #{age,jdbcType=INTEGER}, #{gender,jdbcType=INTEGER}, #{phone,jdbcType=VARCHAR}, ",
        " #{email,jdbcType=VARCHAR}, #{qq,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, ",
        " #{status,jdbcType=INTEGER}, #{accountNonLocked,jdbcType=BIT}, ",
        " #{credentialsNonExpired,jdbcType=BIT}, #{accountNonExpired,jdbcType=BIT}, ",
        " #{enabled,jdbcType=BIT}, #{createTime,jdbcType=TIMESTAMP}, ",
        " #{modifyTime,jdbcType=TIMESTAMP}, #{roleId,jdbcType=INTEGER}, ",
        " #{lastpasswordresetdate,jdbcType=TIMESTAMP})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insert(User record);

    @InsertProvider(type=UserSqlProvider.class, method="insertSelective")
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insertSelective(User record);

    @SelectProvider(type=UserSqlProvider.class, method="selectByExample")
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="username", property="username", jdbcType=JdbcType.VARCHAR),
        @Result(column="password", property="password", jdbcType=JdbcType.VARCHAR),
        @Result(column="age", property="age", jdbcType=JdbcType.INTEGER),
        @Result(column="gender", property="gender", jdbcType=JdbcType.INTEGER),
        @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
        @Result(column="email", property="email", jdbcType=JdbcType.VARCHAR),
        @Result(column="QQ", property="qq", jdbcType=JdbcType.VARCHAR),
        @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
        @Result(column="account_non_Locked", property="accountNonLocked", jdbcType=JdbcType.BIT),
        @Result(column="credentials_non_expired", property="credentialsNonExpired", jdbcType=JdbcType.BIT),
        @Result(column="account_non_expired", property="accountNonExpired", jdbcType=JdbcType.BIT),
        @Result(column="enabled", property="enabled", jdbcType=JdbcType.BIT),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="role_id", property="roleId", jdbcType=JdbcType.INTEGER),
        @Result(column="lastPasswordResetDate", property="lastpasswordresetdate", jdbcType=JdbcType.TIMESTAMP)
    })
    List<User> selectByExample(UserExample example);


    @SelectProvider(type=UserSqlProvider.class, method="selectRoleByExample")
    @ResultMap("BaseResultMap")
    List<User> selectRoleByExample(UserExample example);
    @Select({
        "select",
        " id, username, password, age, gender, phone, email, QQ, description, status, ",
        " account_non_Locked, credentials_non_expired, account_non_expired, enabled, create_time, ",
        " modify_time, role_id, lastPasswordResetDate",
        " from t_user",
        " where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="username", property="username", jdbcType=JdbcType.VARCHAR),
        @Result(column="password", property="password", jdbcType=JdbcType.VARCHAR),
        @Result(column="age", property="age", jdbcType=JdbcType.INTEGER),
        @Result(column="gender", property="gender", jdbcType=JdbcType.INTEGER),
        @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
        @Result(column="email", property="email", jdbcType=JdbcType.VARCHAR),
        @Result(column="QQ", property="qq", jdbcType=JdbcType.VARCHAR),
        @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
        @Result(column="account_non_Locked", property="accountNonLocked", jdbcType=JdbcType.BIT),
        @Result(column="credentials_non_expired", property="credentialsNonExpired", jdbcType=JdbcType.BIT),
        @Result(column="account_non_expired", property="accountNonExpired", jdbcType=JdbcType.BIT),
        @Result(column="enabled", property="enabled", jdbcType=JdbcType.BIT),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="role_id", property="roleId", jdbcType=JdbcType.INTEGER),
        @Result(column="lastPasswordResetDate", property="lastpasswordresetdate", jdbcType=JdbcType.TIMESTAMP)
    })
    User selectByPrimaryKey(Long id);

    @UpdateProvider(type=UserSqlProvider.class, method="updateByExampleSelective")
    int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);

    @UpdateProvider(type=UserSqlProvider.class, method="updateByExample")
    int updateByExample(@Param("record") User record, @Param("example") UserExample example);

    @UpdateProvider(type=UserSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(User record);

    @Update({
        "update t_user",
        " set username = #{username,jdbcType=VARCHAR},",
          " password = #{password,jdbcType=VARCHAR},",
          " age = #{age,jdbcType=INTEGER},",
          " gender = #{gender,jdbcType=INTEGER},",
          " phone = #{phone,jdbcType=VARCHAR},",
          " email = #{email,jdbcType=VARCHAR},",
          " QQ = #{qq,jdbcType=VARCHAR},",
          " description = #{description,jdbcType=VARCHAR},",
          " status = #{status,jdbcType=INTEGER},",
          " account_non_Locked = #{accountNonLocked,jdbcType=BIT},",
          " credentials_non_expired = #{credentialsNonExpired,jdbcType=BIT},",
          " account_non_expired = #{accountNonExpired,jdbcType=BIT},",
          " enabled = #{enabled,jdbcType=BIT},",
          " role_id = #{roleId,jdbcType=INTEGER},",
          " lastPasswordResetDate = #{lastpasswordresetdate,jdbcType=TIMESTAMP}",
        " where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(User record);

    @Select({"SELECT ",
            " tu.*,",
            "trole.id as `roles.id`,",
            "trole.text as `roles.text`," ,
            "trole.enabled as `roles.enabled` " ,
            " FROM " ,
            " (" ,
            "SELECT " ,
            "* " ,
            "FROM " ,
            "t_user " ,
            " WHERE " ,
            "username=#{0} or phone=#{0} " ,
            ") tu " ,
            " LEFT JOIN t_user_role tur ON tu.id = tur.user_id " +
            " LEFT JOIN t_role trole ON trole.id = tur.role_id "})
    @ResultMap("BaseResultMap")
    List<User> selectByUserNameOrPhone(String s);


    @Update("update t_user set enabled=#{enabled} where id=#{id}")
    int enableById(@Param("id") Long id,@Param("enabled") Boolean enabled);


    @SelectProvider(type = UserSqlProvider.class, method = "selectUserByUser")
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="username", property="username", jdbcType=JdbcType.VARCHAR),
            @Result(column="password", property="password", jdbcType=JdbcType.VARCHAR),
            @Result(column="age", property="age", jdbcType=JdbcType.INTEGER),
            @Result(column="gender", property="gender", jdbcType=JdbcType.INTEGER),
            @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
            @Result(column="email", property="email", jdbcType=JdbcType.VARCHAR),
            @Result(column="QQ", property="qq", jdbcType=JdbcType.VARCHAR),
            @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column="account_non_Locked", property="accountNonLocked", jdbcType=JdbcType.BIT),
            @Result(column="credentials_non_expired", property="credentialsNonExpired", jdbcType=JdbcType.BIT),
            @Result(column="account_non_expired", property="accountNonExpired", jdbcType=JdbcType.BIT),
            @Result(column="enabled", property="enabled", jdbcType=JdbcType.BIT),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="role_id", property="roleId", jdbcType=JdbcType.INTEGER),
            @Result(column="lastPasswordResetDate", property="lastpasswordresetdate", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="level", property="level", jdbcType=JdbcType.INTEGER),
            @Result(column="province", property="province", jdbcType=JdbcType.VARCHAR),
            @Result(column="city", property="city", jdbcType=JdbcType.VARCHAR),
            @Result(column="region", property="region", jdbcType=JdbcType.VARCHAR)
    })
    Page<MerchantUser> selectByUser(SysController.UserColumns user);
}